//
//  ViewController.m
//  GCD异同串并主队列组合的情况例举
//
//  Created by hyl on 16/7/17.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self asyncCurrent];
//    [self syncCurrent];
//    [self asynSerial];
//    [self synSerial];
//    [self asyncMain];
    [self syncMain];
}

//主队列中的同步
//1.点击后无作何的效果，但可以确定是点了：是阻塞了；syncMain在主线程中执行，当执行到第一个任务时，因为是同步函数，又是在主函数中，所以被阻塞
-(void) syncMain{
    NSLog(@"begin-syncMain");
    //获取主队列
    dispatch_queue_t queue=dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"%@  1--syncMain",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@  2--syncMain",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@  3--syncMain",[NSThread currentThread]);
    });
    NSLog(@"end-syncMain");
}

//主队列中的异步
//由于是在主队列中添加到主队列中执行，所以即使是异步也不会开启线程
//而且是依次执行每个队列
-(void) asyncMain{
    NSLog(@"begin-asyncMain");
    //获取主队列
    dispatch_queue_t queue=dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"%@  1--asyncMain",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@  2--asyncMain",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@  3--asyncMain",[NSThread currentThread]);
    });
    NSLog(@"end-asyncMain");
}

//串行与同步结合
//1.同步不会开启新线程
//2.其它的都说过
//
-(void) synSerial{
    NSLog(@"begin-asynSerial");
    //    dispatch_queue_t queue=dispatch_queue_create("asynSerial", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue=dispatch_queue_create("asynSerial", NULL);
    dispatch_sync(queue, ^{
        NSLog(@"%@  1--synSerial",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@  2--synSerial",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@  3--synSerial",[NSThread currentThread]);
    });
    
    NSLog(@"end-asynSerial");
}


//#define DISPATCH_QUEUE_SERIAL NULL 等价
//串行与异步结合
//1.异步会开启线程
//2.异步仍然是先执行完begin与end,再执行队列，异步非阻塞
//3.但不再是主线程中执行
-(void) asynSerial{
    NSLog(@"begin-asynSerial");
//    dispatch_queue_t queue=dispatch_queue_create("asynSerial", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue=dispatch_queue_create("asynSerial", NULL);
    dispatch_async(queue, ^{
        NSLog(@"%@  1--asynSerial",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@  2--asynSerial",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@  3--asynSerial",[NSThread currentThread]);
    });
    
    NSLog(@"end-asynSerial");
}

//并行与同步结合
//1.同步时：不会开启新的线程
//2.同步时：会阻塞，第一个任务没有执行完，不会向下再执行；可以保证任务依次执行
//3.队列：先进先出的原则；
-(void) syncCurrent{
    NSLog(@"begin-syncCurrent");
    dispatch_queue_t queue=dispatch_queue_create("asyncCurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        NSLog(@"%@ 1--",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@ 2--",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"%@ 3--",[NSThread currentThread]);
    });
    NSLog(@"end-syncCurrent");
}


//并行与异步结合
//1.异步时：将所有任务添加到队列中，并且不会阻塞，begin与end先打印出来
//2.队列：先进先出的原则；只不过哪一个任务行执行的更快
//3.异步可以开启队列(也有不能开启的时候)
-(void) asyncCurrent{
    NSLog(@"begin");
    //获取全局的队列
//    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t queue=dispatch_queue_create("asyncCurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"%@ 1--",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ 2--",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@ 3--",[NSThread currentThread]);
    });
    NSLog(@"end");
}
@end
